﻿using System;
using System.Windows.Forms;
using AMM.Business;
using AMM.Business.Config;
using AMM.CustomClassLibrary;
using AMM.Model;

namespace AMM.Forms.Config {
    public partial class FrmConfig : FrmRootBase {

        private ConfigurationProcessor _configurationProcessor;

        public FrmConfig() {
            this.InitializeComponent();
        }

        private void btnSyncData_Click(object sender, EventArgs e) {
            switch (((Button)sender).Name) {
                case "btnSyncData":
                    try {
                        new BaseProcessor().SyncData();
                        this.ShowInfo("作业操作存储过程执行成功！");
                    }
                    catch (Exception ex) {
                        Logger.Error(ex.Message, ex);
                        this.ShowInfo("作业操作存储过程执行失败！");
                    }
                    break;
                case "btnClearBasics":
                    try {
                        new BasicsProcessor().Delete();
                        this.ShowInfo("清理未启用基本资料操作执行成功！");
                    }
                    catch (Exception ex) {
                        Logger.Error(ex.Message, ex);
                        this.ShowInfo("清理未启用基本资料操作执行失败！");
                    }
                    break;
            }
        }

        private void btnSave_Click(object sender, EventArgs e) {
            this._configurationProcessor.Update(new Configuration() {
                KeyCode = "AttachmentFolder",
                KeyValue = this.tbAttachmentFolder.Text.Trim()
            });

            this._configurationProcessor.Update(new Configuration() {
                KeyCode = "AttachmentSize",
                KeyValue = this.tbAttachmentSize.Text.Trim()
            });

            this._configurationProcessor.Update(new Configuration() {
                KeyCode = "Offline",
                KeyValue = this.cbOffline.Checked.ToString().ToLower()
            });

            this._configurationProcessor.Update(new Configuration() {
                KeyCode = "Interval",
                KeyValue = this.tbInterval.Text.Trim()
            });

            this._configurationProcessor.Update(new Configuration() {
                KeyCode = "AutoCloseTimes",
                KeyValue = this.tbAutoCloseTimes.Text.Trim()
            });

            this._configurationProcessor.Update(new Configuration() {
                KeyCode = "Upgrade",
                KeyValue = this.cbUpgrade.Checked.ToString().ToLower()
            });

            this.ShowInfo("保存成功！");
        }

        private void FrmConfig_Load(object sender, EventArgs e) {
            this._configurationProcessor = new ConfigurationProcessor();

            this.tbAttachmentFolder.Text = this._configurationProcessor.FetchConfigurationByKeyCode("AttachmentFolder").KeyValue;
            this.tbAttachmentSize.Text = this._configurationProcessor.FetchConfigurationByKeyCode("AttachmentSize").KeyValue;

            this.cbOffline.Checked = bool.Parse(this._configurationProcessor.FetchConfigurationByKeyCode("Offline").KeyValue);
            this.tbInterval.Text = this._configurationProcessor.FetchConfigurationByKeyCode("Interval").KeyValue;
            this.tbAutoCloseTimes.Text = this._configurationProcessor.FetchConfigurationByKeyCode("AutoCloseTimes").KeyValue;

            this.cbUpgrade.Checked = bool.Parse(this._configurationProcessor.FetchConfigurationByKeyCode("Upgrade").KeyValue);

        }
    }
}
